local function oncurrentexp(self, current)
    self.inst.replica.expbooster:SetCurrentExp(current)
end

local function onmaxlevel(self, max)
    self.inst.replica.expbooster:SetMaxLevel(max)
end

local function oncurrentlevel(self, current)
    self.inst.replica.expbooster:SetCurrentLevel(current)
end

local ExpBooster = Class(function(self, inst)
        self.inst = inst
        self.exp = 0
        self.level = 0
        self.max_level = 100
        self.exp_per_level = 100

        inst:DoTaskInTime(0, function()
            self:ApplyStats()
        end)

        inst:ListenForEvent("killed", function(inst, data)
            self:AddExp(1)
            -- TheNet:Announce(self.inst.player_classified.assn_currentexp:value())
        end)

        inst:ListenForEvent("oneat", function(inst, data)
            if data.food and data.food.prefab then
                if data.food.prefab == "assn_suipian" then
                    self:AddExp(2)
                elseif data.food.prefab == "mandrakesoup" then
                    self:AddExp(10)
                    if self.inst.components.stamina then
                        local max = self.inst.components.stamina.max
                        self.inst.components.stamina:SetCurrent(max)
                    end
                elseif data.food.prefab == "nightmarefuel" then
                    self:AddExp(1)
                    if self.inst.components.stamina then
                        local current = self.inst.components.stamina.current
                        self.inst.components.stamina:SetCurrent(current + 10)
                    end
                end
            end
        end)
    end,
    nil,
    {
        exp = oncurrentexp,
        max_level = onmaxlevel,
        level = oncurrentlevel,
    })

function ExpBooster:AddExp(amount)
    if self.level >= self.max_level then return end

    self.exp = self.exp + amount
    while self.exp >= self.exp_per_level and self.level < self.max_level do
        self.exp = self.exp - self.exp_per_level
        self.level = self.level + 1
        self:ApplyStats()
    end
end

function ExpBooster:ApplyStats()
    local base = 100 + self.level * 10
    local maxstamina = 80 + self.level * 5
    if self.inst.components.health then
        self.inst.components.health.maxhealth = base
    end
    if self.inst.components.sanity then
        self.inst.components.sanity.max = base
    end
    if self.inst.components.hunger then
        self.inst.components.hunger.max = base
    end
    if self.inst.components.stamina then
        self.inst.components.stamina.max = maxstamina
    end
end

function ExpBooster:OnSave()
    return {
        exp = self.exp,
        level = self.level,
        assn_hunger = self.inst.components.hunger and self.inst.components.hunger.current or 100,
        assn_health = self.inst.components.health and self.inst.components.health.currenthealth or 100,
        assn_sanity = self.inst.components.sanity and self.inst.components.sanity.current or 100,
    }
end

function ExpBooster:OnLoad(data)
    if data then
        self.exp = data.exp or 0
        self.level = data.level or 0
        self:ApplyStats()
        if self.inst.components.hunger then
            self.inst.components.hunger:SetCurrent(data.assn_hunger or 100)
        end
        if self.inst.components.health then
            self.inst.components.health:SetCurrentHealth(data.assn_health or 100)
        end
        if self.inst.components.sanity then
            self.inst.components.sanity.current = data.assn_sanity or 100
        end
    end
end

return ExpBooster
